import {
  theOrder,
  cancel,
  fetch,
  setPlayComPlet,
  getPayShow
} from '../../config/https'; //订单列表,取消订单,确认收货,订单支付,微信支付签名
let Tool = require('../../config/tool.js');
import Toast from '../../miniprogram_npm/_vant-weapp@0.5.7@vant-weapp/toast/toast';
import Dialog from '../../miniprogram_npm/_vant-weapp@0.5.7@vant-weapp/dialog/dialog';

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    myorderLength: false, //数据为空显示
    finished: false, //是否已经请求完全部数据

    show: false, //确认收货弹出层
    page: 0, //分类页数
    pagesize: 4, //分类每页的返回数
    activeSub: 0, //tab栏默认激活样式
    status: '', //是否付款 物流状态
    dispatchtype: 0, //订单模式
    myorder: [], //订单数据
    forTheGoods: "待收货", //自提订单 待收货 变化
    forTheGoodsto: "待配送", //自提订单 待配送 变化
    navIndex: 1, //订单项颜色切换
    id: '' //点击取消订单的id
  },

  // -----------------------------------------------------------------------------------------------------------------
  navClick(e) { //点击订单项切换 
    let this_ = this;
    let id = e.currentTarget.dataset.id;
    this.setData({
      navIndex: id
    })
    this.dinzTai()
  },
  dinzTai: Tool.debounce(function() { //tab状态
    if (this.data.navIndex == 1) { //配送订单
      this.setData({
        dispatchtype: 0,
        status: '',
        activeSub: 0,
        forTheGoods: "待收货",
        forTheGoodsto: "待配送",
        page: 0,
        myorder: []
      })
      this.nextData() //调用订单列表接口

    } else if (this.data.navIndex == 2) { //自提订单

      this.setData({
        dispatchtype: 1,
        status: '',
        forTheGoods: "退款/售后",
        activeSub: 0,
        forTheGoodsto: "待自提",
        page: 0,
        myorder: []
      })
      this.nextData() //调用订单列表接口
    } else if (this.data.navIndex == 3) { //物流订单

      this.setData({
        dispatchtype: 2,
        status: '',
        activeSub: 0,
        forTheGoods: "待收货",
        forTheGoodsto: "待配送",
        page: 0,
        myorder: []
      })
      this.nextData() //调用订单列表接口
    }
  },300),
  // -----------------------------------------------------------------------------------------------------------------
  onChange(e) { //是否付款 物流状态
    let navIndex = this.data.navIndex; //''：全部 |0：待付款 |1：待取货/待发货 |  2：待收货 |  3：已完成 | 4：售后
    let index = e.detail.index; //订单状态下标
    let this_ = this;

    if (index == 0) { //全部
      this_.setData({
        status: '',
        page: 0,
        myorder: []
      })
      this_.nextData() //调用订单列表接口

    } else if (index == 1) { //待付款 
      this_.setData({
        status: 0,
        page: 0,
        myorder: []
      })
      this_.nextData() //调用订单列表接口

    } else if (index == 2) { //代配送 
      this_.setData({
        status: 1,
        page: 0,
        myorder: []
      })
      this_.nextData() //调用订单列表接口

    } else if (index == 3) { //待收货 
      let navIndex = this_.data.navIndex;
      if (navIndex == 1) {
        this_.setData({
          status: 2,
          page: 0,
          myorder: []
        })
        this_.nextData() //调用订单列表接口
      } else if (navIndex == 2) {
        this_.setData({
          status: 4,
          page: 0,
          myorder: []
        })
        this_.nextData() //调用订单列表接口
      } else if (navIndex == 3) {
        this_.setData({
          status: 2,
          page: 0,
          myorder: []
        })
        this_.nextData() //调用订单列表接口
      }
    } else if (index == 4) { //退款、售后
      this_.setData({
        status: 4,
        page: 0,
        myorder: []
      })
      this_.nextData() //调用订单列表接口
    }
  },
  // -----------------------------------------------------------------------------------------------------------------

  canCel(e) { //                点击取消订单的时候
    let this_ = this;
    let id = e.currentTarget.dataset.id;

    Dialog.confirm({
      message: '确定取消订单吗？',
    }).then(() => { //确定的时候跳转到 订单详情
      Toast("取消成功");

      cancel({
        remark: "不想买了",
        id: id
      }).then(res => {
        console.log("取消订单成功")
      })
      this_.myOrder(); //调用订单列表接口

    })

  },



  // -----------------------------------------------------------------------------------------------------------------
  afterSales(e) { //点击申请售后
    if (!this.pageLoading) {
      this.pageLoading = !0;
      let {
        id,
        status
      } = e.currentTarget.dataset;
      // wx.navigateTo({
      //   url: '/pages/Theorderthrfour/Theorderthrfour?id=' + id + '&status=' + status
      // })
      wx.navigateTo({
        url: '/pages/refund/refund?id=' + id
      })
    }
  },

  toPayFor(e) { //点击 去支付
    if (!this.pageLoading) {
      this.pageLoading = !0;
      let this_ = this;
      let id = e.currentTarget.dataset.id; //点击此项的商品id
      let ordersn = e.currentTarget.dataset.ordersn; //点击此项的商品订单号
      getPayShow({ // 微信支付签名数据
        id: id
      }).then(res => {
        var payInfo = res.sign
        console.log(res)
        wx.requestPayment({ //微信支付
          'timeStamp': payInfo.timeStamp, //时间戳，
          'nonceStr': payInfo.nonceStr, //随机字符串
          'package': payInfo.package, //统一下单接口返回的 prepay_id 参数值
          'signType': payInfo.signType, //签名算法
          'paySign': payInfo.paySign, //签名
          'success': res => {
            Toast("支付成功")
            this.pageLoading = !this.pageLoading;
            // setPlayComPlet({ //支付接口
            //   id: id,
            //   ordersn: ordersn,
            //   type: 'applet',
            // }).then(res => {
            //   console.log("支付成功")
            // })
            this_.myOrder() //调用订单列表接口
          },
          'fail': err => {
            Toast("支付失败")
            this.pageLoading = !this.pageLoading;
          }
        })
      })
    }
  },
  // -----------------------------------------------------------------------------------------------------------------
  details(e) { //点击跳转详情
    let {
      status,
      statusstr,
      id
    } = e.currentTarget.dataset; //点击订单时时当前的订单状态
    //console.log(status)
    this.setData({
      id
    })
    if (this.data.navIndex == 1) { //配送订单
      if (!this.pageLoading) { //阻止多次跳转
        this.pageLoading = !0;
        wx.navigateTo({
          url: '/pages/Theorderthreight/Theorderthreight?id=' + id + "&status=" + status + "&statusstr=" + statusstr
        })
      }
    } else if (this.data.navIndex == 2) { //自提订单
      if (!this.pageLoading) { //阻止多次跳转
        this.pageLoading = !0;
        wx.navigateTo({
          url: '/pages/Sincetheliftfour/Sincetheliftfour?id=' + id + "&status=" + status + "&statusstr=" + statusstr
        })
      }
    } else if (this.data.navIndex == 3) { //物流订单
      if (!this.pageLoading) {
        this.pageLoading = !0;
        wx.navigateTo({
          url: '/pages/Theorderthreight/Theorderthreight?id=' + id + "&status=" + status + "&statusstr=" + statusstr
        })
      }
    }
  },

  // -----------------------------------------------------------------------------------------------------------------
  onClose(e) { //确认收货
    let id = e.currentTarget.dataset.id; //点击此项的商品id
    console.log(id)
    Dialog.confirm({
      message: '是否确认收货？',
    }).then(() => { //确定的时候跳转到 订单详情

      fetch({ //确认收货数据
        id: id
      }).then(res => {
        Toast("成功取货")
        this.myOrder() //调用订单列表接口
      })
    })
  },
  // -----------------------------------------------------------------------------------------------------------------
  myOrder() { //订单列表  刷新
    let this_ = this;
    wx.showLoading({ //加载提示动画
      title: '数据加载中...',
    })
    theOrder({
      page: 1, //页数                             
      status: this_.data.status, //''：全部|0：待付款|1：待取货/待发货 |  2：待收货 |  3：已完成 | 4：售后
      dispatchtype: this_.data.dispatchtype, //0是配送，1是自提
    }).then(res => {
      wx.hideLoading() //加载提示动画消失
      this_.setData({
        myorder: res.list
      })
      if (res.list.length == 0) {
        this_.setData({
          myorderLength: true
        })
      } else {
        this_.setData({
          myorderLength: false
        })
      }
    })
  },
  nextData() { //订单列表 分页
    wx.showLoading({ //加载提示动画
      title: '加载中',
    })
    let this_ = this;
    theOrder({
      page: ++this_.data.page, //页数
      status: this_.data.status, //''：全部|0：待付款|1：待取货/待发货 |  2：待收货 |  3：已完成 | 4：售后
      dispatchtype: this_.data.dispatchtype, //0是配送，1是自提,2物流
    }).then(res => {
      wx.hideLoading() //加载提示动画消失
      if (res.list.length == 0) {
        this_.setData({
          myorderLength: true
        })
      } else {
        this_.setData({
          myorderLength: false
        })
      }
      var myorder = this_.data.myorder;
      myorder = myorder.concat(res.list)
      if (myorder.length == 0) {} else {
        this_.setData({
          finished: res.list.length < res.pagesize ? true : false
        })
      }
      wx.stopPullDownRefresh(); //停止当前页面下拉刷新。
      this_.setData({
        myorder
      })

    }).catch(err => {
      //console.log(err)
    })
  },
  // -----------------------------------------------------------------------------------------------------------------
  onLoad: function (options) {
    /* 生命周期函数--监听页面加载*/
    this.setData({
      navIndex: options.id
    })
    this.dinzTai() //tab状态
    this.myOrder() //调用订单列表接口
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    //this.nextData() //数据列表
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.myOrder() //调用订单列表接口
    this.pageLoading = !1;
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.finished == false) {
      this.nextData();
    }
    return;
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})